import dayjs from "dayjs";
import { action, observable } from "mobx-miniprogram";
const  repeatableDist={
  "Custom":"从不",
  "Daily":"每天",
  "Weekly":"每周",
  "Monthly":"每月"
}
const modes=["从不","每天","每周","每月"]
export const planStore=observable({
  planList:[1],
  weekStatusList:[],
  currentId:0,
  get datePlanList(){
    const planMap=new Map()
    planMap.set("",[])
    planMap.set("今天",[])
    planMap.set("明天",[])
    this.planList.sort((a,b)=>{
      return a.ts-b.ts
    }).forEach(item=>{
      const ts=dayjs(item.ts)
      if(item.top){
        planMap.get("").push(item)
      }else if(dayjs().startOf("D").isBefore(ts)&&dayjs().endOf("D").isAfter(ts)){
        planMap.get("今天").push(item)
      }else if(dayjs().endOf("D").isBefore(ts)&&dayjs().add(1,"day").endOf("D").isAfter(ts)){
        planMap.get("明天").push(item)
      }else{
        if(planMap.has(ts.format("MM-DD"))){
          planMap.get(ts.format("MM-DD")).push(item)
        }else{
          planMap.set(ts.format("MM-DD"),[item])
        }
      }
        
    })
    return Array.from(planMap.entries())
  },
  get planDetail(){
    const {currentId,planList}=this
    if(!currentId)return null
    const res= planList.find(item=>{
      return item.id===currentId
    })
    return {
      ...res,
      date:dayjs(res.ts).format("YYYY-MM-DD"),
      time:dayjs(res.ts).format("HH:mm"),
      isAllDay:res.allDay,
      isMind:res.mind,
      isTop:res.top,
      repetition:repeatableDist[res.repeatable],
      repetitionIndex:modes.indexOf(repeatableDist[res.repeatable])
    }
  },
  setPlanList:action(function(list){
    this.planList=list
  }),
  setWeekStatusList:action(function(list){
    this.weekStatusList=list
  }),
  setId:action(function(id){
    this.currentId=id
  }),
})